package com.niit.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class HDFSClientTest {

    private static Configuration conf = null;
    private static FileSystem fs = null;

    //1. 建立连接
    @Before    //在@Test之前去执行
    public void connect2HDFS() throws IOException {
        //设置客户端的身份 以具备权限在HDFS进行操作
        System.setProperty("HADOOP_USER_NAME","root");

        //创建配置对象实例
        conf = new Configuration();
        //设置指定操作的文件系统是HDFS，并且指定HDFS操作地址
        conf.set("fs.defaultFS","hdfs://192.168.1.128:8020");
        //创建文件系统
        fs = FileSystem.get(conf);
    }

    //2. 执行操作
    //2.1 创建文件夹
    @Test
    public void mkdir() throws IOException {
        //首先判断文件夹是否存储，如果不存在就创建
        if(!fs.exists(new Path("/mydir"))){
            fs.mkdirs(new Path("/mydir"));
        }
    }


    //上传文件
    @Test
    public void putFile2HDFS() throws IOException {
        //创建本地问价路径 hdfs上传路径
        Path src = new Path("D:\\wyf_ftp.txt");
        //上传路径
        Path dst = new Path("/dir1/1.1txt");
        //上传
        fs.copyFromLocalFile(src,dst);
    }

    //2.3 下载
    @Test
    public void getFile2Local() throws IOException {
        // 创建本地文件路径
        Path src = new Path("/dir1/1.1txt");
        //创建上传路径
        Path dst = new Path("D:\\untitled");
        //下载
        fs.copyToLocalFile(src,dst);

    }

    //遍历目录下的内容
    @Test
    public void fileList() throws IOException {
        //遍历指定目录
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
        //输出
        for (FileStatus fileStatus : fileStatuses) {
            System.out.println(fileStatus.toString());
        }
    }

    //创建文件
    @Test
    public void createFile() throws IOException {
        //创建文件
        Path path = new Path("text.txt");
        //利用IO流进行写入文件
        FSDataOutputStream out = fs.create(path);
        out.write("Hello Hadoop".getBytes());
        out.close();
    }

    //读取文件
    @Test
    public void readFile() throws IOException {

        Path path = new Path("text.txt");

        FSDataInputStream open = fs.open(path);
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = open.read(bytes))!=-1){
            String res = new String(bytes,0,len);
            System.out.println(res);
        }
    }

    @Test
    public void deleteFile() throws IOException{
        Path path = new Path("/text.txt");
        if(fs.exists( path )){
            fs.delete(path,false);
        }
    }

    // 3. 关闭连接
    @After   //在@Test之后去执行
    public void close() {
        if(fs != null){
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
